package com.wangzhixuan.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.commons.utils.StringUtils;
import com.wangzhixuan.mapper.AnalysisMapper;
import com.wangzhixuan.model.Analysis;
import com.wangzhixuan.model.vo.OpearteVo;
import com.wangzhixuan.model.vo.ReasonVo;
import com.wangzhixuan.service.IAnalysisService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-07-26
 */
@Service
public class AnalysisServiceImpl extends ServiceImpl<AnalysisMapper, Analysis> implements IAnalysisService {
	
	@Autowired
    private AnalysisMapper analysisMapper;
    
	@Override
    public void selectDataGrid(PageInfo pageInfo) {
    	Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<Analysis> list = analysisMapper.selectAnalysisPage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

	@Override
	public boolean insertReasons(ReasonVo reasonVo) {
		Analysis analysis = null;
		for(String id : reasonVo.getRonIdArrlist()){
			if (StringUtils.isNotBlank(id)) {
				analysis = new Analysis();
				analysis.setCreateId(reasonVo.getUserId());
				analysis.setWorkId(reasonVo.getWorkId());
				analysis.setTypePhenomenonId(reasonVo.getPonId());
				analysis.setTypeReasonId(Integer.parseInt(id));
				analysis.setCreateTime(new Date());
				analysisMapper.insert(analysis);
			}
		}
		return true;
	}

	@Override
	public boolean updateOpearte(OpearteVo opearteVo) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("ronId", opearteVo.getRonId());
		map.put("workId", opearteVo.getWorkId());
		map.put("status", opearteVo.getRonOperationState()-1);
		analysisMapper.updateOpearte(map);
		return true;
	}
}
